package controlsystem;


import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import managesystem.Student;

import com.google.gson.Gson;



public class showCurrentMilestoneServlet extends HttpServlet {
		/*
		 * Deze klasse regelt het verkeer tussen de jsp milestones en de milestoneController. 
		 * @author Christiaan
		 */
	
	static Milestone currentMilestone; 
	
	private static final long serialVersionUID = 1L;
		/*
		 * 
		 */
		
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		currentMilestone=null;
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		
		
		
			
           int positie=Integer.valueOf(req.getParameter("id")).intValue();
         
           Milestone gevonden=MilestoneController.findMilestoneByPosition(currStud,positie);
           currentMilestone=gevonden;
           
           
           String value="";
           String progress="";
           if(gevonden.getType().equals("Number of hours")){
			    int hours=(int)gevonden.getProgress()/360000000;
			    double minutes=(double)gevonden.getProgress()/360000000;
			     value= "Number of hours you want to finish: "+gevonden.getValue()/360000000;
			     progress="Time done: "+((Integer)hours).toString()+ "Hours"+(int)((minutes-(double)hours)*60)+"Minutes";
           }
           if (gevonden.getType().equals("Number of pages")){
        	   value="Number of pages you want to finish: "+gevonden.getValue();
               progress=gevonden.getType()+" done: "+gevonden.getProgress();
           }
           SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
           
           
          String[] list = new String[6];
           list[0]= gevonden.getCourse().getName();
           list[1]= value;
           list[2]=progress;
           list[3]=gevonden.getDescription();
           list[4]=("End date: "+sdf.format(gevonden.getEndDate()));
           list[5]=("Creation date: "+sdf.format(gevonden.getDate()));
           //
   	       String json = new Gson().toJson(list);  	       
	       resp.setContentType("application/json");  
   	       resp.setCharacterEncoding("UTF-8"); 
	       resp.getWriter().write(json); 
	       
	       
	    
	       
			
			
			
			  
           }
	
	public static Milestone getCurrentMilestone()
	{
		
		return currentMilestone;
	}
	
	public static void setCurrentMilestone(Milestone milestone)
	{
		currentMilestone=milestone;
		
	}
	
            


		
	}

		
		




